/**
 * Created by ZFL on 2017/12/21.
 */
var talentId;
var app;
var vm=new Vue({
    el:'#recommend',
    data: {
        coverImg: '',   //封面图
        goodsList: [],  //商品列表(横划显示)
        serialContentImages: [], //期资讯的图片列表
        associateGoods:[],      //关联商品
        goodsListW:0,
        serialId:0
    }

})

function PageInit() {
    if ($.getPara("serialId") != '' || $.getPara("talentSerialId") == '') {
        //普通期
        vm.serialId=$.getPara("serialId");
        expectDetail();
        associateGoods();
        infiniteSroll();   //滚动加载
    }else if ($.getPara("shareSerialId") != '' && $.getPara("talentId")!="" || $.getPara("shareTalentSerialId") != ''&& $.getPara("talentId")!="") {
        //分享期
        talentId=$.getPara("talentId");   //滚动加载
        shareExpect();
    }
}
function  openAllGoods() {
    $('.recommend-wrap').removeClass('back-show');
    $('.all-goods').addClass('back-show');
}
function selectGoods() {
    $('.recommend-wrap').addClass('back-show');
    $('.all-goods').removeClass('back-show ');
}
//加载普通期详情
function expectDetail() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            if (com_obj.userId) {
                obj.userId = com_obj.userId;
            } else {
                obj.userId = 0;
            }
            obj.serialId = $.getPara("serialId");
            return obj;
        },
        visibleTime: 0,
        visible: 'show',
        approve: true,
        api: "yg_baijiaapp/serial/nomalSrialDetail",
        success: function (result) {
            console.log(result);
            //if (result.code == 10000) {
            //    //0
            //    var data = result.data;
            //    var obj ={};
            //    //如果没有微信大图则使用期封面图
            //    if(data.wxSmallImgpath==''){
            //        obj.Imgpath =data.coverImgpath;//期封面图
            //    }else{
            //        obj.Imgpath =data.wxSmallImgpath;//微信大图
            //    }
            //    obj.serialId = data.serialId;//期ID
            //    obj.title = data.title; //期标题
            //    document.title = data.title;
            //    //详情图
            //    var serialContentImages =[];
            //    $.each(data.serialContentImages,function (j,rows) {
            //        var serObj = {};
            //        serObj.imagePath = rows.imagePath; //详情图地址
            //        serObj.imgHeight = rows.imgHeight;
            //        serObj.imgWidth = rows.imgWidth;
            //        serObj.jumpStyle = rows.jumpStyle;
            //        serObj.jumpTo = rows.jumpTo;
            //        if(rows.jumpStyle == 1){//1不跳转，2跳转到商品详情页，3跳转到H5，4跳转到另一期的详情页
            //            serObj.hrefLink = 'javascript:;';
            //        }else if(rows.jumpStyle == 2){
            //            serObj.hrefLink = '/b2c/goods/goods.html?goodsId='+rows.jumpTo+'';
            //        }else if(rows.jumpStyle == 3){
            //            serObj.hrefLink = '/';
            //        }else if(rows.jumpStyle == 4){
            //            serObj.hrefLink = '/recommend/index.html?serialId='+rows.jumpTo+'';
            //        }
            //        serObj.orderNo =  rows.orderNo;
            //        serObj.serialContentId =  rows.serialContentId;
            //        serObj.serialId =  rows.serialId;
            //        serialContentImages.push(serObj);
            //    });
            //    obj.serialContentImages = serialContentImages;
            //    //关联商品
            //    var goodsList =[];
            //    var goods =[];
            //    $.each(data.goodsList,function (j,rows) {
            //        var gObj = {};
            //        gObj.goodsChannel = rows.goodsChannel; //商品进货渠道 1电商；2门店；3海淘
            //        gObj.goodsId = rows.goodsId;//商品ID
            //        gObj.goodsImage = rows.goodsImage;//商品图片
            //        gObj.isYiYe = rows.isYiYe; //是否是异业 0：自营 1：异业 2：海淘
            //        gObj.labelName = rows.labelName;  //标签
            //        gObj.lowestPrice =  rows.lowestPrice;
            //        gObj.name =  rows.name;//商品名称
            //        gObj.suggestedPrice =  rows.suggestedPrice;//市场价
            //        gObj.talentLimit =  rows.talentLimit; //达人佣金比例  百分单位
            //        gObj.href = '/b2c/goods/goods.html?goodsId='+rows.goodsId+'';
            //        goodsList.push(gObj);
            //        if(j<4){
            //            goods.push(gObj);
            //        }
            //    });
            //    var mWidth =9.2;
            //    var _width = 2.7466*(goodsList.length+1)+0.2*(goodsList.length+2);
            //    if(_width>mWidth){
            //        mWidth =_width;
            //    }
            //    obj.mWidth = {width:''+mWidth+'rem'};
            //    var goodsListW=0;
            //    goodsListW=3*goodsList.length+3+'rem';
            //    $('.shop-menus .hot-list-box').css({
            //        'width':goodsListW
            //    })
            //    console.log(goodsListW)
            //    obj.goodsList =goodsList;
            //    obj.goods =goods;
            //    app = new Vue({
            //        el: '#recommendApp',
            //        data: {
            //            app:obj
            //        },
            //        // 在 `methods` 对象中定义方法
            //        methods: {
            //            greet: function (event) {
            //                // `this` 在方法里指当前 Vue 实例
            //                //alert('Hello ' + this.name + '!');
            //                // `event` 是原生 DOM 事件
            //                if (event) {
            //                    //alert(event.target.tagName);
            //                }
            //            }
            //        }
            //    });
            //
            //}
            //else {
            //    $.toast("加载期详情失败","text");
            //}
            if(result.code==10000){
                var data=result.data;
                //如果没有微信大图则使用期封面图
                    if(data.wxSmallImgpath==''){
                       vm.coverImg=data.coverImgpath;//期封面图
                    }else{
                        vm.coverImg =data.wxSmallImgpath;//微信大图
                    }
                data.goodsList.forEach(function(value,index){
                    value.lowestPrice=divideNumber(value.lowestPrice,100);
                    value.href='../goods/goods.html?goodsId='+value.goodsId;
                    vm.goodsList.push(value)
                })

                data.serialContentImages.forEach(function(value,index){
                    if(value.jumpStyle == 1){//1不跳转，2跳转到商品详情页，3跳转到H5，4跳转到另一期的详情页
                                    value.hrefLink = 'javascript:;';
                    }else if(value.jumpStyle == 2){
                                    value.hrefLink = '../goods/goods.html?goodsId='+value.jumpTo+'';
                    }else if(value.jumpStyle == 3){
                                    value.hrefLink = '/';
                    }else if(value.jumpStyle == 4){
                                    value.hrefLink = '/recommend/index.html?serialId='+value.jumpTo+'';
                                }
                    vm.serialContentImages.push(value)
                })
                //$('.shop-menus-rowa a').attr('href','allGoods.html?serialId='+ $.getPara('serialId'))

            }
        }
    });
}

//期资讯下面的商品列表
var pageSize=10;
var pageNum=1;
function associateGoods() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.pageSize=pageSize;
            obj.pageNum=pageNum;
            if (com_obj.userId) {
                obj.userId = com_obj.userId;
            } else {
                obj.userId = 0;
            }
            obj.serialId = $.getPara("serialId");
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        visible: 'show',
        approve: true,
        api:"yg_baijiaapp/serial/getSrialMoreGoodsList",
        success: function (result) {
            if(result.code==10000){
                var data=result.data;
                if(data.length>0){
                    //加载
                    loading=false;
                    data.forEach(function(value,index){
                        value.lowestPrice=divideNumber(value.lowestPrice,100);
                        value.href='../goods/goods.html?goodsId='+value.goodsId;
                        vm.associateGoods.push(value)
                    })
                }else{
                    loading=true;
                    $(document.body).destroyInfinite();
                }
            }

        }
    });
}

//滚动加载(今日推荐)
function infiniteSroll() {
    loading = false;
    $("body").infinite().on("infinite", function () {
        if (loading) return;
        loading = true;
        pageNum++;
        $('.weui-infinite-scroll-box').show();
        setTimeout(function () {
            loading = false;
            $('.weui-infinite-scroll-box').hide();
            associateGoods();
        }, 1500);   //模拟延迟
    });
}
//分享期详情、分享达人期详情
function shareExpect() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            if($.getPara("shareSerialId")!=""){
                obj.shareSerialId = $.getPara("shareSerialId")
            }else if($.getPara("shareTalentSerialId")!=""){
                obj.shareTalentSerialId = $.getPara("shareTalentSerialId");
            }
            return obj;
        },
        visibleTime: 0,
        visible: 'show',
        approve: true,
        api: "yg_baijiaapp/serial/getShare",
        success: function (result) {
            console.log(result);
            if (result.code == 10000) {
                var data = result.data;
                var obj ={};
                obj.agio = data.agio;//分佣类型  1 零佣； 2半佣； 3 全拥
                obj.coverImgpath =data.coverImgpath;//期封面图
                obj.serialId = data.serialId;//期ID
                obj.title = data.title; //期标题
                document.title = data.title;
                //详情图
                var serialContentImages =[];
                $.each(data.resContent,function (j,rows) {
                    var serObj = {};
                    serObj.imagePath = rows.imagePath; //详情图地址
                    serObj.imgHeight = rows.imgHeight;
                    serObj.imgWidth = rows.imgWidth;
                    serObj.jumpStyle = rows.jumpStyle;
                    serObj.jumpTo = rows.jumpTo;
                    if(rows.jumpStyle == 1){
                        serObj.hrefLink = 'javascript:;';
                    }else if(rows.jumpStyle == 2){
                        serObj.hrefLink = '/b2c/goods/goods.html?goodsId='+rows.jumpTo+'';
                    }else if(rows.jumpStyle == 3){
                        serObj.hrefLink = '/';
                    }else if(rows.jumpStyle == 4){
                        serObj.hrefLink = '/recommend/index.html?serialId='+rows.jumpTo+'';
                    }
                    serObj.orderNo =  rows.orderNo;
                    serObj.serialContentId =  rows.serialContentId;
                    serObj.serialId =  rows.serialId;
                    serialContentImages.push(serObj);
                });
                obj.serialContentImages = serialContentImages;
                //关联商品
                var goodsList =[];
                var goods =[];
                $.each(data.goodsList,function (j,rows) {
                    var gObj = {};
                    gObj.goodsChannel = rows.goodsChannel; //商品进货渠道 1电商；2门店；3海淘
                    gObj.goodsId = rows.goodsId;//商品ID
                    gObj.goodsImage = rows.goodsImage;//商品图片
                    gObj.isYiYe = rows.isYiYe; //是否是异业 0：自营 1：异业 2：海淘
                    gObj.labelName = rows.labelName;  //标签
                    gObj.lowestPrice =  rows.lowestPrice;
                    gObj.name =  rows.name;//商品名称
                    gObj.suggestedPrice =  rows.suggestedPrice;//市场价
                    gObj.talentLimit =  rows.talentLimit; //达人佣金比例  百分单位
                    gObj.href = '/b2c/goods/goods.html?goodsId='+rows.goodsId+'&talentId='+$.getPara("talentId")+'&agio='+data.agio+'';
                    goodsList.push(gObj);
                    if(j<4){
                        goods.push(gObj);
                    }
                });
                var mWidth =9.2;
                var _width = 2.7466*(goodsList.length+1)+0.2*(goodsList.length+2);
                if(_width>mWidth){
                    mWidth =_width;
                }
                obj.mWidth = {width:''+mWidth+'rem'};
                obj.goodsList =goodsList;
                obj.goods =goods;
                app = new Vue({
                    el: '#recommendApp',
                    data: {
                        app:obj
                    },
                    // 在 `methods` 对象中定义方法
                    methods: {
                        greet: function (event) {
                            // `this` 在方法里指当前 Vue 实例
                            //alert('Hello ' + this.name + '!');
                            // `event` 是原生 DOM 事件
                            if (event) {
                                //alert(event.target.tagName);
                            }
                        }
                    }
                });

            } else {
                $.toptip(result.message,"error");
                $("#recommendApp").hide();
            }
        }
    });
}


